package com.card323.crpg.io.swing;

import java.awt.Color;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.text.BadLocationException;
import javax.swing.text.DefaultStyledDocument;
import javax.swing.text.Style;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyleContext;
import javax.swing.text.StyledDocument;

import com.card323.crpg.Engine;
import com.card323.crpg.ListenerProvider;
import com.card323.crpg.hero.HeroAPListener;
import com.card323.crpg.hero.HeroStatusListener;
import com.card323.crpg.io.MessageReceiver;
import com.card323.crpg.io.MessageSender;
import javax.swing.BoxLayout;
import javax.swing.JTextField;
import javax.swing.JSplitPane;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import javax.swing.JTextPane;
import javax.swing.JScrollPane;

public class IOJFrame extends JFrame implements MessageReceiver, MessageSender,ListenerProvider {

	/**
	 * 
	 */
	private static final long serialVersionUID = -541788141055156551L;
	private JPanel contentPane;
	private final JTextField textField = new JTextField();
	private final JTextPane textPane = new JTextPane();
	StyledDocument styledDoc = new DefaultStyledDocument();
	private volatile boolean _is_waiting_message;
	private volatile String _message;
	private volatile String _lock = new String();
	private final JScrollPane scrollPane = new JScrollPane();
	private final JSplitPane splitPane = new JSplitPane();
	private final JPanel panel = new JPanel();
	private final JSplitPane battlePane = new JSplitPane();
	

	PlayerPane _allies_pane=new PlayerPane();
	PlayerPane _enemy_pane=new PlayerPane();
	public synchronized boolean is_waiting_message() {
		return _is_waiting_message;
	}

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					String email = "card323@gmail.com";
					Thread t = new Thread(new Engine(email, "swing"));
					t.start();
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public IOJFrame() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 800, 600);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(new BoxLayout(contentPane, BoxLayout.X_AXIS));
		battlePane.setOrientation(JSplitPane.VERTICAL_SPLIT);
		battlePane.setDividerLocation(400);

		contentPane.add(battlePane);
		battlePane.setLeftComponent(panel);
		splitPane.setOrientation(JSplitPane.VERTICAL_SPLIT);
		splitPane.setDividerLocation(110);
		splitPane.setRightComponent(textField);
		textField.addKeyListener(new KeyAdapter() {
			@Override
			public void keyTyped(KeyEvent e) {
				if (e.getKeyChar() == '\n') {
					synchronized (_lock) {

						_message = textField.getText();
						textField.setText("");
						_is_waiting_message = false;
						_lock.notifyAll();
					}

				}
			}
		});
		textField.setColumns(10);
		splitPane.setLeftComponent(scrollPane);
		scrollPane.setViewportView(textPane);
		textPane.setEditable(false);

		textPane.setStyledDocument(styledDoc);
		textPane.setText(">");
		battlePane.setRightComponent(splitPane);
		IOJFrame.create_style("DefaultStyle", styledDoc, 12, 0, 0, 0,
				Color.BLACK, "宋体");

		
		panel.add(_allies_pane);
		panel.add(_enemy_pane);
	}

	public void send_message(String msg) {
		IOJFrame.insert_doc(styledDoc, msg + "\n", "DefaultStyle");
		textPane.setCaretPosition(textPane.getText().length());
	}

	public void send_message(String msg, Color c) {
		IOJFrame.create_style("ColorStyle", styledDoc, 12, 0, 0, 0, c, "宋体");
		IOJFrame.insert_doc(styledDoc, msg + "\n", "ColorStyle");
		textPane.setCaretPosition(textPane.getText().length());
	}

	public String get_message() {
		synchronized (_lock) {
			_message = null;
			_is_waiting_message = true;
			try {
				while (_message == null)
					_lock.wait();
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		return _message;

	}

	public static void create_style(String style, StyledDocument doc, int size,
			int bold, int italic, int underline, Color color, String fontName) {
		Style sys = StyleContext.getDefaultStyleContext().getStyle(
				StyleContext.DEFAULT_STYLE);
		try {
			doc.removeStyle(style);
		} catch (Exception e) {
		} // 先删除这种Style,假使他存在

		Style s = doc.addStyle(style, sys); // 加入
		StyleConstants.setFontSize(s, size); // 大小
		StyleConstants.setBold(s, (bold == 1) ? true : false); // 粗体
		StyleConstants.setItalic(s, (italic == 1) ? true : false); // 斜体
		StyleConstants.setUnderline(s, (underline == 1) ? true : false); // 下划线
		StyleConstants.setForeground(s, color); // 颜色
		StyleConstants.setFontFamily(s, fontName); // 字体
	}

	public static void insert_doc(StyledDocument styledDoc, String content,
			String currentStyle) {
		try {
			styledDoc.insertString(styledDoc.getLength(), content,
					styledDoc.getStyle(currentStyle));
		} catch (BadLocationException e) {
			System.err.println("BadLocationException: " + e);
		}
	}

	public void send_error_message(String msg) {
		send_message("error:"+msg,Color.RED);
		
	}

	public HeroStatusListener get_allies_status_listener() {
		return _allies_pane.get_hero_status_pane();
	}
	
	public HeroAPListener get_allies_ap_listener(){
		return _allies_pane.get_hero_ap_pane();
	}
	public HeroStatusListener get_enemy_status_listener() {
		return _enemy_pane.get_hero_status_pane();
	}
	
	public HeroAPListener get_enemy_ap_listener(){
		return _enemy_pane.get_hero_ap_pane();
	}


}
